package com.kawa.kemanage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kawa.kemanage.entity.dto.ReadMessageDTO;
import com.kawa.kemanage.entity.SystemMessage;
import com.kawa.kemanage.entity.vo.UnreadCountVO;
import com.kawa.kemanage.exception.BadSqlException;
import com.kawa.kemanage.mapper.SystemMessageMapper;
import com.kawa.kemanage.service.SystemMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: KAIyo
 * @Date: 2025/6/20 10:11
 * @Version: v1.0.0
 * @Description: TODO
 **/
@Service
public class SystemMessageServiceImpl implements SystemMessageService {
    @Autowired
    private SystemMessageMapper systemMessageMapper;

    private static final String[] UNREAD_TYPE = {"reminder", "system","activity"};

    /**
     * 获取未读消息数量
     * @param userId 用户ID
     * @return
     * @throws BadSqlException
     */
    @Override
    public UnreadCountVO getUnreadCount(Integer userId) throws BadSqlException {
        //新建对象
        UnreadCountVO unreadCountVO = new UnreadCountVO();
        try {
            //根据不同的type获取未读消息数量
            for (String type : UNREAD_TYPE) {
                unreadCountVO.setTypeCount(type, systemMessageMapper.getUnreadCountByType(type, userId));
            }
            //计算总数
            unreadCountVO.sumUnreadCount();
        }catch (Exception e){
            throw new BadSqlException("getUnreadCount", "select * from system_message where user_id = #{userId} and status = 'unread'", e.getMessage(), e);
        }
        //返回总数
        return unreadCountVO;
    }

    /**
     * 获取系统消息
     *
     * @param userId
     * @param type
     * @param pageNum
     * @param pageSize
     * @return
     * @throws BadSqlException
     */
    @Override
    public PageInfo<SystemMessage> getSystemMessages(Integer userId, String type, Integer pageNum, Integer pageSize) throws BadSqlException {
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<SystemMessage> pageInfo = null;
        try {
            pageInfo = new PageInfo<>(systemMessageMapper.getSystemMessages(userId));
        }catch (RuntimeException runtimeException){
            throw new BadSqlException("getSystemMessages", "select * from system_message where user_id = #{userId} and type = #{type}", runtimeException.getMessage(), runtimeException);
        }
        return pageInfo;
    }


    /**
     * 已读消息，可以直接修改单个或者批量已读状态
     * @param readMessageDTO
     * @throws BadSqlException
     */
    @Override
    public void readSystemMessage(ReadMessageDTO readMessageDTO) throws BadSqlException {
        try {
            //前端传递过来userid用以已读全部消息
            systemMessageMapper.readSystemMessage(readMessageDTO);
        }catch (Exception e){
            throw new BadSqlException("readSystemMessage", "update system_message set status = 'read' where message_id = #{messageId} and user_id = #{userId}", e.getMessage(), e);
        }
    }
}
